#include "Encoding.h"

UnicodeEncoding::UnicodeEncoding( bool permable ) : m_peramble( permable )
{

}

int32 UnicodeEncoding::GetCharSize( uint32& charSize )
{
	charSize = 1;
	return R_SUCCEED;
}

int32 UnicodeEncoding::GetByteSize( uint32& byteSize )
{
	byteSize = 2;
	return R_SUCCEED;
}

int32 UnicodeEncoding::Decode( cbyte* bytes , uint32 byteSize , tchar* chars , uint32 charSize , uint32& useCharSize , uint32& useByteSize )
{
	int32 code = R_SUCCEED;


	if( (byteSize % 2) != 0 )
		byteSize -= 1;

	uint32 uCharSize = 0;
	GetMaxCharSize( byteSize , uCharSize );

	if( uCharSize > charSize )
	{
		useCharSize = uCharSize;
		return E_ENCODING_BUFFESIZE;
	}

	memcpy( (void*)chars , (void*)bytes , byteSize );

	useByteSize = byteSize;
	useCharSize = byteSize / sizeof(tchar);

	return R_SUCCEED;
}

int32 UnicodeEncoding::Encode( ctchar* chars , uint32 charSize , byte* bytes , uint32 byteSize , uint32& useCharSize , uint32& useByteSize )
{
	int32 code = R_SUCCEED;

	uint32 copySize = 0;
	GetMaxByteSize( charSize , copySize );

	if( copySize > byteSize )
	{
		useByteSize = copySize;
		return E_ENCODING_BUFFESIZE;
	}


	memcpy( (void*)bytes ,(void*)chars , copySize );

	useByteSize = copySize;
	useCharSize = charSize;

	return R_SUCCEED;
}

int32 UnicodeEncoding::GetCodePage( EncodingCodePage& codePage )
{
	codePage = CODE_PAGE_UNICODE;
	return R_SUCCEED;
}

int32 UnicodeEncoding::GetPerambleSize( uint32& perambleSize )
{
	if( m_peramble )
		perambleSize = 2;
	else
		perambleSize = 0;
	return R_SUCCEED;
}

int32 UnicodeEncoding::GetPeramble( byte* bytes , cuint32& byteSize )
{
	if( !m_peramble  )
		return R_SUCCEED;
	if( byteSize < 2 )
		return E_ENCODING_PREMABLE_SIZE;

	bytes[0] = 0xFF;
	bytes[1] = 0xFE;

	return R_SUCCEED;
}